//
//  DepartTableViewCell.m
//  gdemm
//
//  Created by wu yong on 16/9/29.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "DepartTableViewCell.h"
#import "Masonry.h"

@interface DepartTableViewCell ()
@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * expandBtn;
@property(nonatomic,strong)UIView * lineView;
@end

@implementation DepartTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.expandBtn = [[UIButton alloc]init];
        [self.expandBtn setImage:[Utils getImageByDevce:@"expand_icon"] forState:UIControlStateNormal];
        
       self.expandBtn.transform = CGAffineTransformIdentity;
        [self.contentView addSubview:self.expandBtn];
        [self.expandBtn addTarget:self action:@selector(expandClick) forControlEvents:UIControlEventTouchUpInside];
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.textColor = [Utils UIColorFromHex:0X323232];
        if ([DeviceTypeJudge isPadDeviceType]) {
           self.titleLabel.font = [UIFont systemFontOfSize:18];
            self.expandBtn.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
        }else{
           self.titleLabel.font = [UIFont systemFontOfSize:15];
            self.expandBtn.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);
        }
        
        [self.contentView addSubview:self.titleLabel];
        UIView * lineView = [[UIView alloc]init];
        self.lineView = lineView;
        lineView.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
        [self.contentView addSubview:lineView];
    }
    return self;
}

-(void)expandClick{
   
    self.node.expanded = !self.node.expanded;
    
    if ([self.delegate respondsToSelector:@selector(expandBtnclick:)]) {
        [self.delegate expandBtnclick:self.index];
    }
}

-(void)setNode:(Node *)node{
    _node = node;
    if (self.node.expanded) {
         self.expandBtn.transform = CGAffineTransformIdentity;
     
    }else{
        self.expandBtn.transform = CGAffineTransformRotate(self.expandBtn.transform,-M_PI/2);
    }
    self.titleLabel.text = node.name;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    if ([DeviceTypeJudge isPadDeviceType]) {
        [self.expandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(self.node.depth*30+35);
            make.centerY.mas_equalTo(self);
            make.width.height.mas_equalTo(55);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.expandBtn.mas_right).offset(10);
            make.centerY.mas_equalTo(self);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(30);
            make.right.mas_equalTo(self.mas_right).offset(-30);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(1);
        }];
    }else{
        [self.expandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(self.node.depth*6+12);
            make.centerY.mas_equalTo(self);
            make.width.height.mas_equalTo(30);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.expandBtn.mas_right).offset(15);
            make.centerY.mas_equalTo(self);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(10);
            make.right.mas_equalTo(self.mas_right).offset(-10);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(1);
        }];

    }
   }


@end
